#include "home.h"
#include "ui_home.h"
#include "manageform.h"
#include "flowcontroler.h"

#include <QTimer>
#include <QDateTime>
#include <QThread>

Home::Home(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Home)
{
    ui->setupUi(this);

    setWindowTitle("动漫交流平台");

    //this->setWindowFlag(Qt::FramelessWindowHint);
    top = new Top();
    ManageForm *manageform = new ManageForm();
    luntan = new Luntan();
    list = new List();
    myself = new Myself();

    ui->stackedWidget->addWidget(top);
    ui->stackedWidget->addWidget(manageform);
    ui->stackedWidget->addWidget(luntan);
    ui->stackedWidget->addWidget(list);
    ui->stackedWidget->addWidget(myself);

    btnGroup.addButton(ui->top_Tb, 0);
    btnGroup.addButton(ui->btnManage, 1);
    btnGroup.addButton(ui->luntan_Tb, 2);
    btnGroup.addButton(ui->list_Tb, 3);
    btnGroup.addButton(ui->myself_Tb, 4);

    connect(&btnGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), [&](int index) {
            qDebug() << "单击编号为 " << index;
            ui->stackedWidget->setCurrentIndex(index);
        });

    btnGroup.button(0)->setChecked(true);
    ui->stackedWidget->setCurrentIndex(0);

    connect(&tcpsocket,SIGNAL(connected()),this,SLOT(showConnect()));
    //connect(&tcpsocket,SIGNAL(readyRead()),this,SLOT(recvMsg()));

//    tcpsocket.connectToHost(QHostAddress("127.0.0.1"),8888);

    QMenu *menu = new QMenu(ui->toolButton);
    auto action = menu->addAction("注销");
    connect(action, &QAction::triggered, this, &Home::onLogoutAction);
    ui->toolButton->setMenu(menu);

    QTimer *timer = new QTimer(this);
    timer->setInterval(500);
    connect(timer, &QTimer::timeout, this, [=](){ui->labTime->setText(QDateTime::currentDateTime().toString(DATETIME_FORMAT));});
    timer->start();
//    ui->stackedWidget->setCurrentIndex(0);

    connect(FlowControler::Interface, &XInterface::logoutFinished, this, [=](int ret)
    {
        if(ret == NoError)
        {
            close();
            FlowControler::Interface->filter =  Core::Filter();
            emit logout();
        }
    });

    QPushButton *button = ui->stackedWidget->widget(0)->findChild<QPushButton *>("pushButton");
    if (button) {
        connect(button, &QPushButton::clicked, this, &Home::toList);
    }
    QPushButton *button2 = ui->stackedWidget->widget(0)->findChild<QPushButton *>("pushButton_2");
    if (button) {
        connect(button2, &QPushButton::clicked, this, &Home::toList);
    }

}

Home::~Home()
{
    delete ui;
}

Home &Home::getInstance()
{
    static Home instance;
    return instance;
}

void Home::showConnect()
{
    //QMessageBox::information(this,"连接服务器","连接服务器成功！");
    qDebug() << "服务器连接成功";
}

void Home::onLoginSuccess(QString name, int permission)
{
    ui->toolButton->setText(name);

    FlowControler::Interface->getAllAnimes();

    if(name != "admin")
    {
        ui->btnManage->setVisible(false);
    }
    else
    {
        FlowControler::Interface->getPendingComment();
        FlowControler::Interface->getPendingPost();
    }
    myself->setUser(*FlowControler::Interface->getCurrentUser());


//    QThread::msleep(500);
    FlowControler::Interface->getPosts(10);
    Core::Filter f;
    FlowControler::Interface->browseByTag(f);
}

void Home::toList()
{
    btnGroup.button(3)->setChecked(true);
    ui->stackedWidget->setCurrentIndex(3);
}

void Home::onLogoutAction()
{
    FlowControler::Interface->logout(*FlowControler::Interface->getCurrentUser());
}





//void Home::on_regist_Pb_clicked()
//{
//    Regist::getInstance().show();
//}

//void Home::on_login_Pb_clicked()
//{
//    Login::getInstance().show();
//}
